# coding:utf-8
class Admin::WifiLogsController < AdminController
	
  respond_to :html, :json

  set_tab :statistics
  set_tab :wifi_logs, :sub_nav
  before_filter :setup, only: [:index]

  def index
    params[:wifi_log] ||= {}
    @wifi_logs = WifiLog.includes(:account).includes(:wifi_account).order('wifi_logs.id DESC')
    @wifi_logs = @wifi_logs.where('wifi_accounts.branch' => params[:wifi_log][:branch]) if params[:wifi_log][:branch].present?
    @wifi_logs = @wifi_logs.where('wifi_accounts.username' => params[:wifi_log][:username]) if params[:wifi_log][:username].present?
    @wifi_logs = @wifi_logs.where('accounts.mobile_number' => params[:wifi_log][:mobile_number]) if params[:wifi_log][:mobile_number].present?
    if params[:wifi_log][:used] == 'true'
      @wifi_logs = @wifi_logs.where('wifi_logs.end_at IS NULL')
    elsif params[:wifi_log][:used] == 'false'
      @wifi_logs = @wifi_logs.where('wifi_logs.end_at IS NOT NULL')
    end
    @wifi_logs = @wifi_logs.where('wifi_logs.mac' => params[:wifi_log][:mac]) if params[:wifi_log][:mac].present?
    @wifi_logs = @wifi_logs.where('wifi_logs.imei' => params[:wifi_log][:imei]) if params[:wifi_log][:imei].present?
    @wifi_logs = @wifi_logs.where(["wifi_logs.start_at >= ?", params[:wifi_log][:start_at]]) if params[:wifi_log][:start_at].present?
    @wifi_logs = @wifi_logs.where(["wifi_logs.start_at < ?", 1.days.since(Time.zone.parse(params[:wifi_log][:end_at]))]) if params[:wifi_log][:end_at].present?
	end

  def new
    @wifi_log = WifiLog.new
  end

  def edit
    @wifi_log = WifiLog.find params[:id]
  end

  def create
    @wifi_log = WifiLog.new(params[:wifi_log])
    render :action => :new unless @wifi_log.save
  end

  def update
    @wifi_log = WifiLog.find params[:id]
    render :action => :edit unless @wifi_log.update_attributes(params[:wifi_log])
  end

  def destroy
    @wifi_log = WifiLog.find params[:id]
    @wifi_log.destroy
  end

  private

  def setup
    @ways = ["统计","公共热点日志"]
    @left_panel = "layouts/statistics_left_panel"
  end
end